package com.znsb.msfq.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.znsb.msfq.R;
import com.znsb.msfq.utils.DisplayUtil;
import com.znsb.msfq.utils.ImageLoader;
import com.znsb.msfq.utils.OnItemClickListener;
import com.znsb.msfq.view.selectorphoto.Bimp;

import java.io.File;

/**
 * @name： 孙科技
 * @phone： 13308334117
 * @createTime： 2017/2/20
 * @modifyTime： 2017/2/20
 * @explain：
 */
public class AddShowBillAdapter extends RecyclerView.Adapter {
    private static final int ITEM_VIEW_HEADER = 0;
    private static final int ITEM_VIEW_CONTENT = 1;
    private Context mContext;

    public AddShowBillAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void updateList() {
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        if (viewType == ITEM_VIEW_HEADER) {
//            return new TextViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_add_image, null));
//        } else if (viewType == ITEM_VIEW_CONTENT) {
//            return new ItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.plugin_published_grida, null));
//        }
        return new ItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_image, null));
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ItemViewHolder) {
            ViewGroup.LayoutParams p = ((ItemViewHolder) holder).imageView.getLayoutParams();
            p.width = DisplayUtil.dip2px(100);
            p.height = DisplayUtil.dip2px(100);
            ((ItemViewHolder) holder).imageView.setLayoutParams(p);
            ((ItemViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            File file = new File(Bimp.tempSelectBitmap.get(position).getImagePath());
            ImageLoader.imageLoder(((ItemViewHolder) holder).imageView, file);
            ((ItemViewHolder) holder).imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onItemClickListener.onItemClick(v, position);
                }
            });
        }
//        else if (holder instanceof TextViewHolder) {
//            ((TextViewHolder) holder).textView.setVisibility(View.VISIBLE);
//            ((TextViewHolder) holder).textView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    onItemClickListener.onItemClick(v, position);
//                }
//            });
//        }
    }

    @Override
    public int getItemCount() {
        return Bimp.tempSelectBitmap.size();
//        return Bimp.tempSelectBitmap.size() == 0 ? 0 : Bimp.tempSelectBitmap.size() + 1;
    }

//    @Override
//    public int getItemViewType(int position) {
//        if (position == 0) {
//            return ITEM_VIEW_HEADER;
//        } else {
//            return ITEM_VIEW_CONTENT;
//        }
//    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;

        public ItemViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.image);
        }
    }

    class TextViewHolder extends RecyclerView.ViewHolder {
        public TextView textView;

        public TextViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.item_tv_addimage);
        }
    }
}
